package com.bdc.whatwhat.task;

import android.os.AsyncTask;

import com.bdc.whatwhat.Service.Service;

/**
 * Created by florentchampigny on 25/06/14.
 */
public class AimerTask extends AsyncTask<String, Void, Void> {

    public interface AimerTaskDelegate {
        public void afficherChargement(boolean afficher);
        public void onAimerEnvoye();
        public void onErreurReseau();
    }

    AimerTaskDelegate delegate;
    boolean erreurReseau;

    public AimerTask(AimerTaskDelegate delegate) {
        this.delegate = delegate;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (delegate != null)
            delegate.afficherChargement(true);
    }

    @Override
    protected Void doInBackground(String... objects) {
        String id = objects[0];
        String token = objects[1];

        try {
            Service.aimer(id,token);
        } catch (Service.NetworkException e) {
            erreurReseau = true;
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        super.onPostExecute(v);
        if (delegate != null) {
            delegate.afficherChargement(false);
            if (erreurReseau)
                delegate.onErreurReseau();
            else
                delegate.onAimerEnvoye();
        }
    }
}
